<template>
    <div id="login">
      <div id="header">
        <span>账号登录</span>
      </div>
      <div id="body">
        <el-form :rules="rules"
                 v-loading="loading"
                 element-loading-text="正在登录..."
                 element-loading-spinner="el-icon-loading"
                 element-loading-background="rgba(0, 0, 0, 0.8)"
                 ref="loginForm"
                 :model="loginForm"
                 class="loginContainer">
          <el-form-item prop="username">
            <el-input type="text" auto-complete="false" v-model="loginForm.username"
                      placeholder="请输入用户名"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input type="password" auto-complete="false" v-model="loginForm.password"
                      placeholder="请输入密码"></el-input>
          </el-form-item>
          <el-form-item prop="captcha">
            <el-input type="text" auto-complete="false" v-model="loginForm.captcha" placeholder="点击图片更换验证码"
                      style="width: 250px;margin-right: 5px;float: left" @keyup.enter.native="submitLogin" ></el-input>
            <img :src="captchaUrl" @click="updateCaptcha" style="float: left">
          </el-form-item>
          <el-button type="primary" style="width: 100%" @click="submitLogin">登录</el-button>
        </el-form>
      </div>
    </div>
</template>

<script>

export default {
  name: "Login",
  props: ["isLogin"],
  data(){
    return {
      captchaUrl: 'user/captcha?time=' + new Date(),
      loginForm: {
        username: 'cxm',
        password: '123456',
        captcha: ''
      },
      loading: false,
      rules: {
        username: [{required: true, message: '请输入用户名', trigger: 'blur'}],
        password: [{required: true, message: '请输入密码', trigger: 'blur'}],
        captcha: [{required: true, message: '请输入验证码', trigger: 'blur'}]
      }
    }
  },
  methods: {
    updateCaptcha() {
      this.captchaUrl = '/user/captcha?time=' + new Date();
    },
    submitLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading=true;
          this.postRequest('/user/login', this.loginForm).then(resp => {
            this.loading = false;
            if (resp.status===200) {
              //存储用户token
              const token = resp.data.tokenHead + resp.data.token;
              sessionStorage.setItem('token', token);
              sessionStorage.setItem("user",JSON.stringify(resp.data.user));
              //页面跳转
              this.$emit("Login",resp.data.user.admin)
              this.$router.back()
            }
          })
        } else {
          this.$message.error('请输入所有字段！');
          return false;
        }
      });
    }
  }
}
</script>

<style scoped>
#login{
  width: 40%;
  margin: auto;
}
#header{
  text-align: center;
}
#header span{
  display: inline-block;
  padding-bottom: 4px;
  color: #409EFF;
  border-bottom: 3px solid #409EFF;
  font-size: 20px;
}
#body{
  margin-top: 30px;
}

.loginContainer {
  border-radius: 15px;
  background-clip: padding-box;
  margin:  auto;
  width: 350px;
  padding: 15px 35px 15px 35px;
  background: #fff;
}

</style>